package com.woniuxy.happylife.order.controller;

import com.woniuxy.happylife.entity.MallOrder;
import com.woniuxy.happylife.entity.MallOrderItem;
import com.woniuxy.happylife.entity.Order;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Title: OrderController
 * @ProjectName: happy-life
 * @Description: TODO
 * @Author: chowinggum
 * @Date: 2022/2/18 15:37
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    OrderService orderService;
    @Autowired
    RedisTemplate redisTemplate;

    //秒杀商品数量初始化
    @RequestMapping("/killstart")
    public void killStart(){
        redisTemplate.opsForValue().set("stock",100);
    }
    @PostMapping("/killgoods/{uid}")
    public void killGoods(@PathVariable("uid") int uid){
        orderService.killGoods(uid);
    }

    @PostMapping("/payorder/{uid}")
    public void payOrder(@PathVariable("uid") int uid){
        redisTemplate.opsForValue().set(uid+"",1);

    }
    @PostMapping("/addorder")
    public ResponseEntity addOrder(@RequestBody Order order){
        int i = orderService.add(order);
        if(i>0){
            return new ResponseEntity(200,"添加成功","成功添加"+i+"条数据");
        }else {
            return new ResponseEntity(200,"添加失败",null);
        }
    }

    @GetMapping("/findByOrderno/{orderno}")
    public ResponseEntity findByOrderno(@PathVariable("orderno") String orderno){
        MallOrder order = orderService.findByOrderno(orderno);
        if(order==null){
            return new ResponseEntity(401,"查询失败",null);
        }else{
            return new ResponseEntity(200,"查询成功",order);

        }

    }

    @GetMapping("/countByUid/{userid}")
    public ResponseEntity countByUid(@PathVariable("userid") int userid){
        int i = orderService.countByUid(userid);
        if (i>0){
            return new ResponseEntity(200,"查询成功",i);
        }else{
            return new ResponseEntity(401,"查询失败",null);

        }
    }

    @GetMapping("/findByUid/{userid}")
    public ResponseEntity findByUid(@PathVariable("userid") int userid){
        List<MallOrder> orders = orderService.findByUid(userid);
        if (orders!=null){
            return new ResponseEntity(200,"查询成功",orders);
        }else{
            return new ResponseEntity(401,"查询失败",null);

        }
    }

    @GetMapping("/findpartByUid/{}userid")
    public ResponseEntity findpartByUid(@PathVariable("userid") int userid){
        List<MallOrder> orders = orderService.findpartByUid(userid);
        if (orders!=null){
            return new ResponseEntity(200,"查询成功",orders);
        }else{
            return new ResponseEntity(401,"查询失败",null);

        }
    }

    @PostMapping("/addItem")
    public ResponseEntity addItem(@RequestBody MallOrderItem orderitem){
        int i = orderService.addItem(orderitem);
        if(i>0){
            return new ResponseEntity(200,"添加成功","成功添加"+i+"条数据");
        }else {
            return new ResponseEntity(401,"添加失败",null);
        }
    }

    @GetMapping("/findItemByOrderId/{orderId}")
    public ResponseEntity findItemByOrderId(@PathVariable("orderId") int orderId){
        List<MallOrderItem> items = orderService.findItemByOrderId(orderId);
        if (items!=null){
            return new ResponseEntity(200,"查询成功",items);
        }else{
            return new ResponseEntity(401,"查询失败",null);

        }
    }

    @DeleteMapping("/delOrder/{orderno}")
    public ResponseEntity delOrder(@PathVariable("orderno")String orderno){
        int i = orderService.delOrder(orderno);
        if(i>0){
            return new ResponseEntity(200,"删除成功","成功删除"+i+"条数据");
        }else {
            return new ResponseEntity(401,"删除失败",null);
        }
    }

}
